<?php
namespace Home\Controller;
class MediaController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("media");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}

	//账号列表
	public function wemedia_list()
	{
		// p($_GET);
		$db_wemedia = M("wemedia");
		$user_name = I('get.user_name');
		$source = I('get.source');
		$sourcetime = I('get.sourcetime');
		$where = " 1=1 and status=2";
		if (!empty($user_name)) {$where .= " and (username like '%$user_name%')";}
		if (!empty($source)&&!empty($sourcetime)) {
			$strat = $sourcetime;
			$end = date("Y-m-d",strtotime($strat)+86400);
			$where .= " and source={$source} and (creationtime BETWEEN '{$strat}' and '{$end}')";
		}
		$count = $db_wemedia->where($where)->count();
		$p = getpage($count, 20);
		$wemedialist = $db_wemedia->where($where)->field("password",true)->limit($p->firstRow, $p->listRows)->select();
		foreach ($wemedialist as $key => &$value) {
			$value['is_articles'] = 0;
			$data = M("media")->where(array('wid'=>$value['wid'],'articles'=>"-17-"))->find();
			if ($data) {
				$value['is_articles'] = 1;
			}
		}
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = I('get.user_name');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($wemedialist);exit();
		$this->assign('wemedialist',$wemedialist);
		$this->assign('page', $page);
		$this->display();
	}

	//自媒体列表
	public function media_list()
	{
		// p($_GET);
		$db_media = M("media m");
		$user_name = I('get.user_name');
		$labelkey = I('get.labelkey');
//		$subscript = I('get.subscript')?1:0;
		$wid = I('get.wid')?I('get.wid'):0;
		 $this->user_name = $user_name;
		 $this->labelkey = $labelkey;
//		 $this->subscript = $subscript;
		$where = " 1=1 ";
		if (!empty($user_name)) {$where .= " and (media_tel like '%$user_name%' or mmid like '%$user_name%' or media_name like '%$user_name%')";}
		if (!empty($labelkey)) {$where .= " and (articles like '%$labelkey%')";}
//		if (!empty($subscript)) {$where .= " and (subscript = 1)";}
		if (!empty($wid)) {$where .= " and (wid = $wid)";}
		$count = $db_media->where($where)->count();
		$p = getpage($count, 20);
		$medialist = $db_media->where($where)
            ->field('m.*,o.ordernum')
            ->join('(SELECT COUNT(*) AS ordernum,media_id FROM `db_order` WHERE ( (status=1 and pay_status=1) or status in (3,5,6,7,8,9)) GROUP BY `media_id`) as o on o.media_id = m.id','left')
            ->limit($p->firstRow, $p->listRows)->select();
		$labeldata = M("label")->select();//读取配置
		foreach ($medialist as $key => &$value) {
			$value['nickname'] = "无";
			if ($value['uuid']) {
				$value['nickname'] = M('member')->field("nickname")->where("uuid = $value[uuid]")->find()['nickname'];
			}
//            $value['ordernum'] = M('order')->where(array("media_id"=>$value['id']))->where("(status=1 and pay_status=1) or status=3 or status=5 or status=7 or status=9")->count()+0;
			$value['signtimelimit'] = round($value['signtimelimit']/3600,2);
		}
//		dump($medialist);die;
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = I('get.user_name');
		$map['labelkey'] = I('get.labelkey');
//		$map['subscript'] = I('get.subscript')?1:0;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($medialist);exit();
		$this->assign('medialist',$medialist);
		$this->assign('page', $page);
		$this->assign('labeldata',$labeldata);
		$this->display();
	}
	//自媒体订单列表
	public function media_order()
	{
		// p($_POST);exit();
		$db_order = M("order");
		$media_id = I('media_id');
		$order_name = I('order_name');
		$this->media_id = $media_id;
		$this->labelkey = $labelkey;
		$this->order_name = $order_name;
		$this->levelkey = $levelkey;
		$where = "media_id = $media_id ";
		if (!empty($labelkey)) {$where .= " and `texttype` = \"".$labelkey."\" ";}
		if (!empty($levelkey)) {$where .= " and `level` = \"".$levelkey."\" ";}
		if (!empty($order_name)) {$where .= " and (media_name like '%$order_name%' or id like '%$order_name%' or title like '%$order_name%')";}
		// echo $where;exit();
		$count = $db_order->where($where)->order('id desc')->count();
		$p = getpage($count, 20);
		$orderlist = $db_order->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		// $leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$labeldata = M("label")->select();//读取配置
		foreach ($orderlist as $key => &$value) {
			// foreach ($leveldata as $k => $v) {
			// 	if($value['level'] == $v['id']){$value['level_type'] = $v['message'];}
			// }
			$value['usemoney'] = $value['price'];
			if ($value['requirement'] == 3) {
				$value['usemoney'] = $value['price']+$value['increase_price'];
			}
			if ($value['bgm_id'] != 0) {
				$value['usemoney'] = $value['usemoney']+10;
			}
			$value['usemoney'] = sprintf('%.2f', $value['usemoney']);
			$value['mediamoney'] = sprintf('%.2f', $value['usemoney']*2);
		}
		// p($orderlist);exit();
		$this->assign('orderlist',$orderlist);
		$this->assign('labeldata',$labeldata);
		// $this->assign('leveldata',$leveldata);
		$this->assign('page', $p->show());
		$this->display();
	}
	//添加余额
	public function words_add()
	{
		$mmid = I('get.mmid');
		$mediadata = M("media")->where(array('mmid'=>$mmid))->find();
		$payword_array = $this->payword_array();
		$payword_arr = array();
		switch($mediadata['level']){//5顶级 6知名 7优质
			case 5:
				for($i=9;$i<=12;$i++){
					$payword_arr[] = $payword_array[$i];
				}
				break;
			case 6:
				for($a=5;$a<=8;$a++){
					$payword_arr[] = $payword_array[$a];
				}
			case 7:
				for($b=1;$b<=4;$b++){
					$payword_arr[] = $payword_array[$b];
				}
		}
		$this->mmid = $mmid;
		$this->assign('payword_arr',$payword_arr);
		$this->assign('mediadata',$mediadata);
		$this->assign('jumpurl',$_SERVER["HTTP_REFERER"]);
		$this->display();
	}

	//添加余额
	public function add_words()
	{
//		 p($_POST);exit();
		$jumpUrl = I('post.jumpurl');
		$wordnumber = (int)I('post.wordnumber');
		$grantnumber = (int)I('post.grantnumber');
		$remark = I('post.remark');
		$signmoney = I('post.signmoney');
		$sign_payment = I('post.sign_payment');
		$serial_number = I('post.serial_number');
		$pay_state = I('post.pay_state');
		$wordtype = I('post.wordtype');
		$contract_number = I('post.contract_number');//合同号
		$mmid = I('post.mmid');
		$media_info = M('media')->where(['mmid'=>$mmid])->find();
		if(!empty($wordtype)) {
			switch ($media_info['level']) {
				case 5;
					if (!in_array($wordtype, [9, 10, 11, 12])) {
						$this->error('套餐参数错误', "", 1);
					}
					break;
				case 6;
					if (!in_array($wordtype, [5, 6, 7, 8])) {
						$this->error('套餐参数错误', "", 1);
					}
					break;
				case 7;
					if (!in_array($wordtype, [1, 2, 3, 4])) {
						$this->error('套餐参数错误', "", 1);
					}
					break;
				default:
					$this->error('自媒体未选择签约等级', "", 1);
			}
		}
		if(empty($contract_number)){
			//soundems2018010101
			$ymd = date('Ymd',time());
			$end_number = M('wemediaincome')->where(['contract_number'=>['like',"soundems".$ymd."%"]])->order('contract_number DESC')->find()['contract_number'];
			if(empty($end_number)){
				$contract_number = "soundems".$ymd."01";
			}else{
				$str = substr($end_number, -2);
				$str++;
				if($str<10){
					$str = "0".$str;
				}
				$contract_number = "soundems".$ymd.$str;
			}
		}
		/*if (empty($wordnumber)||empty($mmid)) {
			$this->error('参数错误',"",1);
		}*/
		$media = M("media");
		$where['mmid'] = $mmid;
		$number = get_orderid();
		$wordnumber = $wordnumber>0 ? $wordnumber : 0;
		$grantnumber = $grantnumber>0 ? $grantnumber : 0;
		if ($wordnumber>0 || $grantnumber>0) {
			$result1 = $media->where($where)->setInc('wordnumber',$wordnumber);// 加字数
			$result2 = $media->where($where)->setInc('grantnumber',$grantnumber);// 赠字数
			if ($result1 || $result2) {
				$media_info1 = M('media')->field('wordnumber,grantnumber')->where(['mmid'=>$mmid])->find();
				M('wemediaincome')->add(array("creationtime"=>date("Y-m-d H:i:s",time()),"wid"=>$media_info['wid'],"mid"=>$mmid,"money"=>$wordnumber,"grantmoney"=>$grantnumber,"status"=>6,"oid"=>0,"number"=>$number,"remark"=>$remark,"balance"=>$media_info1['wordnumber'],"grantbalance"=>$media_info1['grantnumber'],"signmoney"=>$signmoney,"sign_payment"=>$sign_payment,"serial_number"=>$serial_number,"contract_number"=>$contract_number,"pay_state"=>$pay_state,"wordtype"=>$wordtype));
			}
			$this->adminlog($mmid."充值字数".$wordnumber.",赠送字数".$grantnumber);
		}
		$this->success('充值成功',$jumpUrl,1);


	}
	//创建自媒体
	public function media_add()
	{
		$labeldata = M("label")->select();//读取配置
		$this->article_num = M("systemsetup")->where("id=1")->find()['value'];
		$this->assign('labeldata',$labeldata);
		$this->display();
	}

	//待审核详情
	public function add_media()
	{
		// p($_POST);exit();
		$media_name = I('post.media_name');
		$media_tel = I('post.media_tel');
		$subscript = I('post.subscript');
		$ishot = I('post.ishot');
		$collectpower = I('post.collectpower');
		$timepoint = I('post.timepoint');
		$audio_op = I('post.audio_op');
		$audio_bgm = I('post.audio_bgm');
		$media_pic = I('post.media_pic');
		$pricing = I('post.pricing');
		if (empty($media_name)||empty($media_tel)) {
			$this->error('参数错误',"",1);
		}
		if (!empty($media_pic)) {
			$media_pic = "http://image.heardtech.com/".$media_pic;
		}
		if (!empty($audio_op)) {
			$audio_op = "http://media.heardtech.com/".$audio_op;
		}
		if (!empty($audio_bgm)) {
			$audio_bgm = "http://media.heardtech.com/".$audio_bgm;
		}
		$media = D("media");
		if (!$media->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $media->create(); // 保存生成的数据对象

			$data['create_time'] = date('Y-m-d H:i:s',time());
			$data['media_message'] = I('post.media_message');
			$data['media_pic'] = $media_pic;
			$data['media_name'] = $media_name;
			$data['media_tel'] = $media_tel;
			$data['subscript'] = $subscript;
			$data['ishot'] = $ishot;
			$data['collectpower'] = $collectpower;
			$data['pricing'] = $pricing;
			$data['timepoint'] = $timepoint;
			$data['audio_op'] = $audio_op;
			$data['audio_bgm'] = $audio_bgm;
			$labeldata = M("label")->select();//读取配置
			$articles = I('post.articles');
			$data['articles'] = implode(",", $articles);
			foreach ($labeldata as $k1 => $v1) {
				foreach ($articles as $k2 => $v2) {
					if($v1['label_id'] == $v2){$data['articles_arr'][$k2] = $v1['label_name'];}
				}
			}
			$data['articles_name'] = implode(",", $data['articles_arr']);
			// p($data);exit();
			$result = $media->add($data);
			$mmid['mmid'] = 100000 + (int)$result;
			$mmid['token_key'] = md5((int)$result);
			$media->where(array('id'=>$result))->save($mmid);
			// echo $media->_sql();exit();
			$this->success('创建成功',"/Home/Media/media_list",1);
		}

	}


	//修改标签
	public function media_edit()
	{
		$mmid = I('get.mmid');
		$db_media = M("media");
		$mediadata = $db_media->where(array('mmid'=>$mmid))->find();
		switch($mediadata['level']){
			case 5: $mediadata['level_name'] = "顶级";break;
			case 6: $mediadata['level_name'] = "知名";break;
			case 7: $mediadata['level_name'] = "优质";break;
		}
		$mediadata['articles'] = explode(",", $mediadata['articles']);
		// p($mediadata);exit();
		$labeldata = M("label")->select();//读取配置
		$this->article_num = M("systemsetup")->where("id=1")->find()['value'];
		$deadline = empty($mediadata['deadline'])?time():$mediadata['deadline'];
		$this->assign('mediadata',$mediadata);
		$this->assign('labeldata',$labeldata);
		$this->today = $deadline;
		$this->display();
	}

	public function edit_media()
	{
		$mmid = I('post.mmid');
		$media_name = I('post.media_name');
		$media_tel = I('post.media_tel');
		$subscript = I('post.subscript');
		$ishot = I('post.ishot');
		$collectpower = I('post.collectpower');
		$timepoint = I('post.timepoint');
		$signtimelimit = I('post.signtimelimit')*60;
		$wid = I('post.wid')?I('post.wid'):0;
		$level = I('post.level')?I('post.level'):0;

		if (empty($media_name)||empty($media_tel)) {
			$this->error('参数错误',"",1);
		}
		if ($collectpower==1) {
			// $pricing = I('post.pricing',0,'intval');;
			$deadline = strtotime(I('post.deadline')." 23:59:59");
		}else{
			$deadline = "";
			// $pricing = 0;
		}
		/*if ($collectpower==1&&$pricing == 0) {
			$this->error('价格错误',"",1);
		}*/
		/*if($this->isExpertise('avatar') == true ){
			$avatar = $this->_postQiniu(2,'avatar');
		}*/
		/*$pic = $this->php_upload();
		if (empty($pic)) {
			$media_pic = I('post.media_pic');
		}else{
			$media_pic = $pic;
		}*/
		if (empty(I('post.media_pic'))) {
			$media_pic = I('post.media_pic_old');
		}else{
			$media_pic = "http://image.heardtech.com/".I('post.media_pic');
		}
		if (empty(I('post.audio_op'))) {
			$audio_op = I('post.audio_op_old');
		}else{
			$audio_op = "http://media.heardtech.com/".I('post.audio_op');
		}
		if (empty(I('post.audio_bgm'))) {
			$audio_bgm = I('post.audio_bgm_old');
		}else{
			$audio_bgm = "http://media.heardtech.com/".I('post.audio_bgm');
		}
		$media = D("media");
		if (!$media->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $media->create(); // 保存生成的数据对象
			$data['media_message'] = I('post.media_message');
			$data['media_pic'] = $media_pic;
			$data['media_name'] = $media_name;
			$data['media_tel'] = $media_tel;
			$data['subscript'] = $subscript;
			$data['ishot'] = $ishot;
			$data['collectpower'] = $collectpower;
			// $data['pricing'] = $pricing;
			$data['timepoint'] = $timepoint;
			$data['audio_op'] = $audio_op;
			$data['audio_bgm'] = $audio_bgm;
			if($level != 0){
				$data['level'] = $level;
			}
			$labeldata = M("label")->select();//读取配置
			$articles = I('post.articles');
			$data['articles'] = implode(",", $articles);
			foreach ($labeldata as $k1 => $v1) {
				foreach ($articles as $k2 => $v2) {
					if($v1['label_id'] == $v2){$data['articles_name'][$k2] = $v1['label_name'];}
				}
			}
			$data['articles_name'] = implode(",", $data['articles_name']);
			$data['deadline'] = $deadline;
			$data['signtimelimit'] = $signtimelimit;
			// p($data);exit();
			$media->where(array('mmid'=>$mmid))->save($data);//更新数据
			// echo $media->_sql();exit();
			$this->adminlog("修改自媒体".$mmid);
			$this->success('修改成功',"/Home/Media/media_list?wid=$wid",1);
		}
	}


	//专辑列表
	public function special_list()
	{
		$db_special = M("mediaspecial");
		$mmid = I('get.mmid');
		$this->mmid = $mmid;
		$where = " mediaid=$mmid ";
		$count = $db_special->where($where)->order('id desc')->count();
		$p = getpage($count, 20);
		$speciallist = $db_special->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();

		// p($speciallist);exit();
		$this->assign('speciallist',$speciallist);
		$this->assign('page', $p->show());
		$this->display();
	}

	//创建专辑
	public function special_add()
	{
		$this->mmid = I('get.mmid');
		$this->display();
	}

	//创建专辑
	public function add_special()
	{
		// p($_POST);exit();
		$specialmessage = I('post.specialmessage');
		$specialname = I('post.specialname');
		$specialimg = I('post.media_pic');
		$mediaid = I('post.mmid');
		if (empty($specialname)||empty($specialimg)||empty($mediaid)) {
			$this->error('参数错误',"",1);
		}
		if (!empty($specialimg)) {
			$specialimg = "http://image.heardtech.com/".$specialimg;
		}
		$special = D("mediaspecial");
		if (!$special->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $special->create(); // 保存生成的数据对象

			$data['addtime'] = date('Y-m-d H:i:s',time());
			$data['specialmessage'] = $specialmessage;
			$data['specialname'] = $specialname;
			$data['specialimg'] = "http://image.heardtech.com/".$specialimg;
			$data['mediaid'] = $mediaid;
			// p($data);exit();
			$result = $special->add($data);
			$this->success('创建专辑成功',"/Home/Media/special_list?mmid=$mediaid",1);
		}

	}


	//修改专辑
	public function special_edit()
	{
		// p($_GET);exit();
		$mmid = I('get.mmid');
		$id = I('get.id');
		$this->mmid = $mmid;
		$this->id = $id;
		$db_special = M("mediaspecial");
		$specialdata = $db_special->where(array('id'=>$id))->find();
		// p($specialdata);exit();
		$this->assign('specialdata',$specialdata);
		$this->display();
	}

	//修改专辑
	public function edit_special()
	{
		$id = I('post.id');
		$mediaid = I('post.mmid');
		$specialname = I('post.specialname');
		$specialmessage = I('post.specialmessage');
		if (empty($specialname)||empty($id)||empty($mediaid)) {
			$this->error('参数错误',"",1);
		}
		if (empty(I('post.media_pic'))) {
			$specialimg = I('post.media_pic_old');
		}else{
			$specialimg = "http://image.heardtech.com/".I('post.media_pic');
		}
		$special = D("mediaspecial");
		if (!$special->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $special->create(); // 保存生成的数据对象
			$data['specialimg'] = $specialimg;
			$data['specialname'] = $specialname;
			$data['specialmessage'] = $specialmessage;
			// p($data);exit();
			$special->where(array('id'=>$id))->save($data);//更新数据
			// echo $special->_sql();exit();
			$this->success('修改成功',"/Home/Media/special_list?mmid=$mediaid",1);
		}
	}

	//自媒体列表
	public function media_url()
	{
		$db_media = M("media");
		$user_name = I('post.user_name');
		$labelkey = I('post.labelkey');
		$this->user_name = $user_name;
		$this->labelkey = $labelkey;
		$where = " 1=1 ";
		if (!empty($user_name)) {$where .= " and (media_tel like '%$user_name%' or mmid like '%$user_name%' or media_name like '%$user_name%')";}
		if (!empty($labelkey)) {$where .= " and (articles like '%$labelkey%')";}
		$count = $db_media->where($where)->count();
		$p = getpage($count, 20);
		$medialist = $db_media->where($where)->limit($p->firstRow, $p->listRows)->select();
		$labeldata = M("label")->select();//读取配置
		foreach ($medialist as $key => &$value) {
			$value['mediaurl'] = "http://wechat.heardtech.com/?token_key=".$value['token_key'];
		}
		// p($medialist);exit();
		$this->assign('medialist',$medialist);
		$this->assign('page', $p->show());
		$this->assign('labeldata',$labeldata);
		$this->display();
	}


	//添加余额
	public function balance_add()
	{
		$wid = I('get.wid');
		$wemediadata = M("wemedia")->where(array('wid'=>$wid))->find();
		$this->wid = $wid;
		$this->assign('wemediadata',$wemediadata);
		$this->display();
	}

	//添加余额
	public function add_balance()
	{
		// p($_POST);exit();
		$balance = I('post.balance');
		$grantmoney = I('post.grantmoney');
		$remark = I('post.remark');
		$wid = I('post.wid');
		if (empty($wid)) {
			$this->error('参数错误',"",1);
		}
		$wemedia = M("wemedia");
		$where['wid'] = $wid;
		$number = get_orderid();
		// $scale = $balance > 5000 ? 0.4 : 0.2;
		// $reward = ceil($balance * $scale);      // 赠送金额
		if ($balance>0) {
			$result1 = $wemedia->where($where)->setInc('balance',$balance);// 加余额
			if ($result1) {
				M('wemediaincome')->add(array("creationtime"=>date("Y-m-d H:i:s",time()),"wid"=>$wid,"money"=>$balance,"status"=>2,"oid"=>0,"number"=>$number,"remark"=>$remark));
				$this->adminlog($wid."充值余额".$balance);
			}
		}
		if ($grantmoney>0) {
			$result2 = $wemedia->where($where)->setInc('grantmoney',$grantmoney);// 加余额
			if ($result2) {
				M('wemediaincome')->add(array("creationtime"=>date("Y-m-d H:i:s",time()),"wid"=>$wid,"money"=>$grantmoney,"status"=>11,"oid"=>0,"number"=>$number,"remark"=>$remark));
				$this->adminlog($wid."赠送余额".$grantmoney);
			}
		}
		$this->success('充值成功',"/Home/Media/wemedia_list",1);


	}
	//反馈列表
	public function wemediafeedback_list()
	{
		// p($_GET);
		$db_wemediafeedback = M("wemediafeedback b");
		$user_name = I('get.user_name');
		$where = " type=1 ";
		// if (!empty($user_name)) {$where .= " and (w.username like '%$user_name%')";}
		$count = $db_wemediafeedback->where($where)->count();
		$p = getpage($count, 20);
		$wemediafeedbacklist = $db_wemediafeedback->where($where)->order("b.id desc")->limit($p->firstRow, $p->listRows)->select();
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = I('get.user_name');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($wemediafeedbacklist);exit();
		$this->assign('wemediafeedbacklist',$wemediafeedbacklist);
		$this->assign('page', $page);
		$this->display();
	}
	//来源数据
	public function mediasourcedata()
	{
		$strat = I('post.strat')?(I('post.strat')):date("Y-m-d",time());
		$end = date("Y-m-d H:i:s",strtotime($strat)+86400);
		$db_wemedia = M("wemedia");
		$data = array();
		$where['creationtime'] = array("BETWEEN",array($strat,$end));
		$where['status'] = 2;
		$where['source'] = array("NEQ","0");
		$data = $db_wemedia->where($where)->select();
		$source = $db_wemedia->where($where)->distinct(true)->field("source")->select();
		foreach ($source as $key => &$value) {
			$value['sourcenum'] = 0;
			foreach ($data as $k => &$val) {
				if ($value['source'] == $val['source']) {
					$value['sourcenum'] += 1;
				}
			}
			switch ($value['source']) {
				case '1':$value['sourcename'] = "135编辑器";$value['sourcetype'] = "广告";break;
				case '2':$value['sourcename'] = "壹伴";$value['sourcetype'] = "广告";break;
				case '3':$value['sourcename'] = "ipaiban";$value['sourcetype'] = "广告";break;
				case '4':$value['sourcename'] = "365编辑器";$value['sourcetype'] = "广告";break;
				case '5':$value['sourcename'] = "新媒体管家";$value['sourcetype'] = "广告";break;
				case '98':$value['sourcename'] = "淘淘课";$value['sourcetype'] = "友情链接";break;
				case '97':$value['sourcename'] = "爱微帮";$value['sourcetype'] = "广告位（免费）";break;
				case '96':$value['sourcename'] = "鲸打卡";$value['sourcetype'] = "友情链接";break;
				case '95':$value['sourcename'] = "易电云服";$value['sourcetype'] = "友情链接";break;
				case '94':$value['sourcename'] = "人人链";$value['sourcetype'] = "友情链接";break;
				case '93':$value['sourcename'] = "邮电出版社";$value['sourcetype'] = "友情链接";break;
				case '92':$value['sourcename'] = "135编辑器";$value['sourcetype'] = "友情链接";break;
				case '91':$value['sourcename'] = "新媒体运营";$value['sourcetype'] = "友情链接";break;
				case '90':$value['sourcename'] = "口袋运营";$value['sourcetype'] = "友情链接";break;
				case '89':$value['sourcename'] = "笔神";$value['sourcetype'] = "友情链接";break;
				case '88':$value['sourcename'] = "黑蚂蚁电台";$value['sourcetype'] = "友情链接";break;
				case '87':$value['sourcename'] = "一说FM";$value['sourcetype'] = "友情链接";break;
			}
		}
		$this->strat = $strat;
		// $this->end = $end;
		$this->assign('source',$source);
		$this->display();
	}

public function recruit_list()
{
	$db_wemediafeedback = M("wemediafeedback b");
	$user_name = I('get.user_name');
	$where = " type=2 ";
	$count = $db_wemediafeedback->where($where)->count();
	$p = getpage($count, 20);
	$wemediafeedbacklist = $db_wemediafeedback->where($where)->order("b.id desc")->limit($p->firstRow, $p->listRows)->select();
	$page = $p->show();
	// 获取查询参数
	$map['user_name'] = I('get.user_name');
	$p->parameter = "";
	foreach($map as $key=>$val) {
		$p->parameter .= "$key=".urlencode($val)."&";
	}
	$this->assign('wemediafeedbacklist',$wemediafeedbacklist);
	$this->assign('page', $page);
	$this->display();
}

//发现主播列表
public function find_user_list()
{
	$this->p = I('get.p')?I('get.p'):1;
	$keyword = I('get.keyword');
	$this->keyword = $keyword;
	$where = '';
	if (!empty($keyword)) {$where = "(m.media_name like '%$keyword%' or m.media_tel like '%$keyword%')";}
	$count = M('medialabeluser mu')->where($where)->join('__MEDIA__ m on m.id = mu.mid','left')->join('__MEMBER__ u on u.uuid = mu.uuid','left')->count();
	$p = getpage($count, 20);
	$medialabeluser_list = M('medialabeluser mu')
		->join('__MEDIA__ m on m.id = mu.mid','left')
		->join('__MEMBER__ u on u.uuid = mu.uuid','left')
		->where($where)
		->field('mu.id,m.media_name,m.media_tel,mu.uuid,u.username,u.nickname,mu.addtime')
		->limit($p->firstRow, $p->listRows)
		->order('id DESC')
		->select();
	$page = $p->show();
	$map['keyword'] = empty($keyword)?"":$keyword;
	$p->parameter = "";
	foreach($map as $key=>$val) {
		$p->parameter .= "$key=".urlencode($val)."&";
	}
	$this->assign('medialabeluser_list',$medialabeluser_list);
	$this->assign('page',$page);
	$this->display();
}

	public function income_list(){
		$this->p = I('get.p')?I('get.p'):1;
		$user_name = I('get.user_name');
		$this->user_name = $user_name;
		$where = 'i.status = 13';
		if(!empty($user_name)){
			$where .= " and media_tel like '%$user_name%' or media_name like '%$user_name%'";
		}
		$count = M('wemediaincome i')->where($where)->join('__MEDIA__ m on i.mid = m.mmid')->count();
		$p = getpage($count,20);
		$income_list = M('wemediaincome i')
			->field('m.id,m.mmid,m.media_name,m.media_tel,m.level,u.uuid,u.nickname,u.username,i.money,i.signmoney,i.creationtime')
			->where($where)
			->join('__MEDIA__ m on i.mid = m.mmid')
			->join('__MEMBER__ u on m.uuid = u.uuid','left')
			->limit($p->firstRow,$p->listRows)
			->order('creationtime DESC')
			->select();
		$page = $p->show();
		$map['user_name'] = empty($user_name)?'':$user_name;
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('income_list',$income_list);
		$this->assign('page',$page);
		$this->display();
	}

	//字数转移页面
	public function remove_words(){
		if(IS_AJAX){
			$post = I('post.');
			//判断当前用户字数是否够
			$old_media_info = M('media')->where(['mmid'=>$post['old_mmid']])->find();
			$new_media_info = M('media')->where(['mmid'=>$post['new_mmid']])->find();
			if(!$old_media_info){$this->ApiError('参数错误');}
			if(!$new_media_info){$this->ApiError('转移自媒体错误');}
			if($old_media_info['wid']!=$new_media_info['wid'] || $old_media_info['level']!=$new_media_info['level']){$this->ApiError('自媒体签约等级不相同');}
			if(($old_media_info['wordnumber']+$old_media_info['grantnumber'])<$post['remove_words']){
				$this->ApiError('输入字数大于总字数');
			}
			$time = time();
			$adminid = urlsafe_b64decode(cookie('heardadminData'));
			M()->startTrans();
			if($post['remove_words']>$old_media_info['grantnumber']){
				//赠送扣除，剩余扣一部分
				$deduct = $post['remove_words']-$old_media_info['grantnumber'];
				M('media')->lock(true)->where(['id'=>$old_media_info['id']])->setDec('grantnumber',$old_media_info['grantnumber']);
				M('media')->lock(true)->where(['id'=>$old_media_info['id']])->setDec('wordnumber',$deduct);
				M('media')->lock(true)->where(['id'=>$new_media_info['id']])->setInc('grantnumber',$old_media_info['grantnumber']);
				M('media')->lock(true)->where(['id'=>$new_media_info['id']])->setInc('wordnumber',$deduct);
				$old_media_n = M('media')->where(['mmid'=>$old_media_info['mmid']])->find();
				$new_media_n = M('media')->where(['mmid'=>$new_media_info['mmid']])->find();

				$add_old_res = M('wemediaincome')->add(['creationtime'=>date('Y-m-d H:i:s',$time),'wid'=>$old_media_info['wid'],'mid'=>$old_media_info['mmid'],'status'=>14,'money'=>$deduct,'grantmoney'=>$old_media_info['grantnumber'],'balance'=>$old_media_n['wordnumber'],'grantbalance'=>$old_media_n['grantnumber'],'signmoney'=>$post['signmoney'],'remark'=>$post['remark']]);//插入14扣钱记录

				$add_new_res = M('wemediaincome')->add(['creationtime'=>date('Y-m-d H:i:s',$time),'wid'=>$new_media_info['wid'],'mid'=>$new_media_info['mmid'],'status'=>15,'money'=>$deduct,'grantmoney'=>$old_media_info['grantnumber'],'balance'=>$new_media_n['wordnumber'],'grantbalance'=>$new_media_n['grantnumber'],'signmoney'=>$post['signmoney'],'remark'=>$post['remark']]);//插入15加钱记录

				$res = M('removewords_log')->add(['adminid'=>$adminid,'removeid'=>$add_old_res,'removetoid'=>$add_new_res,'wid'=>$old_media_info['wid'],'words'=>$deduct,'grantwords'=>$old_media_info['grantnumber'],'create_time'=>$time]);
			}else{
				//全部扣赠送部分
				M('media')->lock(true)->where(['id'=>$old_media_info['id']])->setDec('grantnumber',$post['remove_words']);
				M('media')->lock(true)->where(['id'=>$new_media_info['id']])->setInc('grantnumber',$post['remove_words']);
				//先查出来转移完成后的字数
				$old_media_n = M('media')->where(['mmid'=>$old_media_info['mmid']])->find();
				$new_media_n = M('media')->where(['mmid'=>$new_media_info['mmid']])->find();
				//income表中记录14扣字记录和15加字记录
				$add_old_res = M('wemediaincome')->add(['creationtime'=>date('Y-m-d H:i:s',$time),'wid'=>$old_media_info['wid'],'mid'=>$old_media_info['mmid'],'status'=>14,'grantmoney'=>$post['remove_words'],'balance'=>$old_media_n['wordnumber'],'grantbalance'=>$old_media_n['grantnumber'],'signmoney'=>$post['signmoney'],'remark'=>$post['remark']]);//插入14扣钱记录

				$add_new_res = M('wemediaincome')->add(['creationtime'=>date('Y-m-d H:i:s',$time),'wid'=>$new_media_info['wid'],'mid'=>$new_media_info['mmid'],'status'=>15,'grantmoney'=>$post['remove_words'],'balance'=>$new_media_n['wordnumber'],'grantbalance'=>$new_media_n['grantnumber'],'signmoney'=>$post['signmoney'],'remark'=>$post['remark']]);//插入15加钱记录
				$res = M('removewords_log')->add(['adminid'=>$adminid,'removeid'=>$add_old_res,'removetoid'=>$add_new_res,'wid'=>$old_media_info['wid'],'grantwords'=>$post['remove_words'],'create_time'=>$time]);
			}
			M()->execute("UNLOCK TABLES");
			if($res){
				M()->commit();
				$this->adminlog("转移字数:从".$post['old_mmid']."到".$post['new_mmid']);
				$this->ApiSuccess('操作成功');
			}else{
				M()->rollback();
				$this->ApiError('添加失败','999999');
			}
		}else{
			$mmid = I('get.mmid');
			$meida_info = M('media')->where(['mmid'=>$mmid])->find();
			if(!in_array($meida_info['level'],[5,6,7])){
				$this->error('当前自媒体还未选择签约等级');
			}
			$where['wid'] = $meida_info['wid'];
			$where['level'] = $meida_info['level'];
			$where['mmid'] = ['neq',$mmid];
			$media_list = M('media')->where($where)->select();
			$this->assign('media_info',$meida_info);
			$this->assign('jumpurl',$_SERVER["HTTP_REFERER"]);
			$this->assign('media_list',$media_list);
			$this->display();
		}
	}

	//签约套餐配置
	public function payword_array(){
		$payword = array("1"=>array("id"=>1,"money"=>"200","word"=>"10000"),"2"=>array("id"=>2,"money"=>"1100","word"=>"50000"),"3"=>array("id"=>3,"money"=>"2000","word"=>"100000"),"4"=>array("id"=>4,"money"=>"9300","word"=>"500000"),"5"=>array("id"=>5,"money"=>"400","word"=>"10000"),"6"=>array("id"=>6,"money"=>"2500","word"=>"50000"),"7"=>array("id"=>7,"money"=>"4800","word"=>"100000"),"8"=>array("id"=>8,"money"=>"22700","word"=>"500000"),"9"=>array("id"=>9,"money"=>"800","word"=>"10000"),"10"=>array("id"=>10,"money"=>"5000","word"=>"50000"),"11"=>array("id"=>11,"money"=>"9800","word"=>"100000"),"12"=>array("id"=>12,"money"=>"46500","word"=>"500000"));
		return $payword;
	}
/*------------------------------------------*/
}